<?php
    class AdminNet extends iJBlock
    {
        /**
         *
         * This function is construct Languages
         *
         *
         * @access public
         */
        public function __construct() {
        }
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJDataBase =  iJFactory::singleton('iJDataBase');
          $iJUploader =  iJFactory::singleton('iJUploader');
          if(!empty($_FILES) && !empty($_POST['title']) && !empty($_POST['url'])) {
            $iJUploader->upload(DIR_VAR . 'leftBarIcons/');
            $filename = $_FILES['icon']['name'];
            
            if(!empty($_FILES['icon']['name'])) {
                $Qpage = $iJDataBase->query('INSERT INTO :table_net (title, image, sortOrder, url)
                                                VALUES(:title, :filename, :sortOrder, :url)
                                            ON DUPLICATE KEY UPDATE 
                                                image     = :filename, 
                                                sortOrder = :sortOrder,
                                                url       = :url
                                      ');
            } else {
                $Qpage = $iJDataBase->query('INSERT INTO :table_net (title, image, sortOrder, url)
                                                VALUES(:title, :filename, :sortOrder, :url)
                                            ON DUPLICATE KEY UPDATE  
                                                sortOrder = :sortOrder,
                                                url       = :url
                                          ');
            }
            
            
            $Qpage->bindTable(':table_net',     TABLE_NET);
            $Qpage->bindValue(':title',         $_POST['title']);
            $Qpage->bindValue(':filename',      $filename);
            $Qpage->bindValue(':url',           $_POST['url']);
            $Qpage->bindValue(':sortOrder',     (isset($_POST['sortOreder'])?$_POST['sortOreder']:'0'));
            
            $Qpage->execute();
          }
          
          if(isset($_GET['delID']))  {
            $QNews = $iJDataBase->query('DELETE FROM :table_net 
                                        WHERE id = :id');
            $QNews->bindTable(':table_net', TABLE_NET);
            $QNews->bindValue(':id',        $_GET['delID']);
            $QNews->execute();
          }
          
          
          if(isset($_GET['ID'])) {
            $QNews = $iJDataBase->query('SELECT id, title, url, sortOrder, image
                                        FROM :table_net 
                                        WHERE id = :id ');
            $QNews->bindTable(':table_net', TABLE_NET);
            $QNews->bindValue(':id',        $_GET['ID']);
            $QNews->execute();
            if ($QNews->next()) {
              $this->addToken('title',       $QNews->value('title'));
              $this->addToken('url',         $QNews->value('url'));
              $this->addToken('sortOrder',   $QNews->value('sortOrder'));
              $this->addToken('image',       $QNews->value('image'));
              $this->addToken('delete_link', iJHrefLink::linkToLocalPage('admin_net'). '&delID=' . $_GET['ID']);
            }

          }
          
          $QNews = $iJDataBase->query('SELECT id, title, url, sortOrder, image
                                      FROM :table_net 
                                      ORDER BY sortOrder');
          $QNews->bindTable(':table_net', TABLE_NET);
          $QNews->execute();
          $news = array();
          while ($QNews->next()) {
            $news[$QNews->value('id')]['title']      = $QNews->value('title');
            $news[$QNews->value('id')]['url']        = $QNews->value('url');
            $news[$QNews->value('id')]['sortOrder']  = $QNews->value('sortOrder');
            $news[$QNews->value('id')]['image']      = $QNews->value('image');
            $news[$QNews->value('id')]['href']       = iJHrefLink::linkToLocalPage('admin_net') . '&ID=' . $QNews->value('id');
            
            
          }
          
          
          
          $this->addToken('news', $news);
          
          $this->addToken('form_action', iJHrefLink::linkToLocalPage('admin_net'));
          
          return parent::execute();
        }
        
    }
?>